﻿// The Nova Project by Ken Beckett.
// Copyright (C) 2007-2012 Inevitable Software, all rights reserved.
// Released under the Common Development and Distribution License, CDDL-1.0: http://opensource.org/licenses/cddl1.php

using System.Collections.Generic;

using Nova.CodeDOM;

namespace Nova.UI
{
    /// <summary>
    /// The view model for a <see cref="CodeDOM.IndexerDecl"/>.
    /// </summary>
    public class IndexerDeclVM : PropertyDeclBaseVM
    {
        #region /* STATICS */

        internal static void AddViewModelMapping()
        {
            CreateViewModel.Add(typeof(IndexerDecl),
                delegate(CodeObject codeObject, bool isDescription, Dictionary<CodeObject, CodeObjectVM> dictionary) { return new IndexerDeclVM((IndexerDecl)codeObject, isDescription, dictionary); });
        }

        #endregion

        #region /* FIELDS */

        protected ChildListVM<ParameterDeclVM> _parameterVMs;

        #endregion

        #region /* CONSTRUCTORS */

        /// <summary>
        /// Create a view model instance for the specified <see cref="CodeDOM.IndexerDecl"/>.
        /// </summary>
        public IndexerDeclVM(IndexerDecl indexerDecl, bool isDescription, Dictionary<CodeObject, CodeObjectVM> dictionary)
            : base(indexerDecl, isDescription, dictionary)
        {
            _parameterVMs = CreateListVM<ParameterDecl, ParameterDeclVM>(indexerDecl.Parameters, dictionary);
        }

        #endregion

        #region /* PROPERTIES */

        /// <summary>
        /// The underlying <see cref="CodeDOM.IndexerDecl"/> model.
        /// </summary>
        public IndexerDecl IndexerDecl
        {
            get { return (IndexerDecl)CodeObject; }
        }

        /// <summary>
        /// A collection of <see cref="ParameterDeclVM"/>s for the parameters of the indexer.
        /// </summary>
        public ChildListVM<ParameterDeclVM> ParameterVMs
        {
            get { return _parameterVMs; }
        }

        /// <summary>
        /// True if the indexer has parameters.
        /// </summary>
        public bool HasParameterVMs
        {
            get { return (_parameterVMs != null && _parameterVMs.Count > 0); }
        }

        /// <summary>
        /// The number of parameters the indexer has.
        /// </summary>
        public int ParameterVMCount
        {
            get { return (_parameterVMs != null ? _parameterVMs.Count : 0); }
        }

        #endregion

        #region /* METHODS */

        #endregion

        #region /* RENDERING */

        protected override void RenderStatement(CodeRenderer renderer, RenderFlags flags)
        {
            RenderFlags passFlags = (flags & RenderFlags.PassMask);
            if (_typeVM != null)
                _typeVM.Render(renderer, passFlags | RenderFlags.IsPrefix);
            if (flags.HasFlag(RenderFlags.Description) && ParentVM is TypeDeclVM)
            {
                ((TypeDeclVM)ParentVM).RenderName(renderer, flags);
                DotVM.RenderDot(renderer, this);
            }
            if (IndexerDecl.IsExplicitInterfaceImplementation)
                CreateVM(IndexerDecl.ExplicitInterfaceExpression).Render(renderer, passFlags & ~(RenderFlags.Description | RenderFlags.ShowParentTypes));
            else
                CreateVM(IndexerDecl.NameExpression).Render(renderer, passFlags);
        }

        protected override void RenderArgumentPrefix(CodeRenderer renderer, RenderFlags flags)
        { }

        protected override void RenderArgument(CodeRenderer renderer, RenderFlags flags)
        {
            RenderFlags passFlags = (flags & RenderFlags.PassMask);
            renderer.RenderText(IndexerDecl.ParseTokenStart, PUNC_BRUSH, this);
            RenderInfixComments(renderer, 0, flags);
            renderer.RenderList(_parameterVMs, passFlags, this);
            if (IndexerDecl.IsEndFirstOnLine)
                renderer.NewLine();
            renderer.RenderText(IndexerDecl.ParseTokenEnd, PUNC_BRUSH, this);
        }

        public override void RenderVisible(CodeRenderer renderer, RenderFlags flags)
        {
            base.RenderVisible(renderer, flags);
            renderer.RenderVisibleList(_parameterVMs, flags);
        }

        public override void UnRender()
        {
            ChildListHelpers.UnRender(_parameterVMs);
            base.UnRender();
        }

        #endregion
    }
}
